package Long.Lu.config;

import Long.Lu.Service.LoginService;
import Long.Lu.pojo.UserInfo;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @ClassName:
 * @description:
 * @author: yh
 * @createDate: 2020/10/26
 * @version: 1.0
 */
public class MyRealm extends AuthorizingRealm {
    /*注入service*/
    @Autowired
    LoginService loginService;

    /*授权*/
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    /*认证*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        /*1.拿到用户名*/
        UsernamePasswordToken upt = (UsernamePasswordToken) authenticationToken;
        String userName = upt.getPrincipal() + "";
        /*2.从数据库中取出用户的加密的密码*/
        UserInfo userInfo = loginService.selectByName(userName);
        /*3.创建密码校验的凭证对象*/
        AuthenticationInfo auto = new SimpleAuthenticationInfo(
                userInfo.getUserName(), userInfo.getUserPwd(), new Md5Hash(userName), this.getName()
        );
        return auto;
    }
}
